package br.com.knowledge.persistence.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import br.com.knowledge.helper.KnowledgePropertiesReader;
import br.com.knowledge.persistence.hibernate.dao.AcdProjetolinguagemDAO;
import br.com.knowledge.view.struts.objects.DadosSessionJDBC;

public class ConnectionFactory{
	private ConnectionFactory(){

	}
	private static final Logger log = LoggerFactory.getLogger(AcdProjetolinguagemDAO.class);

	public static Connection getConnection( DadosSessionJDBC dadosSessionJBDC ) {

		Connection con = null;

		try {
			Class.forName(dadosSessionJBDC.getDriversgdb());
			String url = dadosSessionJBDC.getConfigsgdb();
			String user = dadosSessionJBDC.getNmusersgdb();
			String senha = dadosSessionJBDC.getPasswordJDBC();
			con=DriverManager.getConnection(url, user, senha);
			return con;
		} catch (SQLException e) {			
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return con;
	}

	public static boolean testConnection(String url, String driver, String user, String pass){

		Connection con = null;

		try {
			if (driver.isEmpty()==false){
				Class.forName(driver);
			}			
			try {
				con = DriverManager.getConnection(url, user, pass);
			} catch (SQLException e) {
				e.printStackTrace();
				String erro = e.getMessage();
				System.out.println(erro);
			}
			if (con==null){
				return false;
			}
			else{
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

		}catch (RuntimeException re) {
			log.error("find by example failed", re);			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return true;
	}
}